package Board;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.*;

import Clock.Clock;
import SubClasses.CellData;

public class Board extends JFrame implements KeyListener 
{
	// Size of the board
	private int ROW;
	private int COL;
	public BoardGuiScreen ser;
	public ArrayList<Character> charList;
	
	public Board(int row,int col)
	{
		ROW = row;
		COL = col;
		charList = new ArrayList<Character>();	
		ser = new BoardGuiScreen(0,0,0,ROW,COL);
		ser.setVisible(false);
		this.getContentPane().add(ser);
		
		this.setLayout(null);
		this.setBounds(0, 0, 934, 562);	
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
				
		
		this.addKeyListener(this);
		this.setFocusable(true);
		this.setVisible(true);
	}
	public void SetNewLevel(int life,int level,int score)
	{
		//BoardGuiScreen(int life,int level,int score,int row,int col)
		this.getContentPane().remove(ser);
		ser = new BoardGuiScreen(life,level,score,ROW,COL);
		ser.setVisible(true);
		this.getContentPane().add(ser);
		this.setFocusable(true);
		this.setVisible(true);
	}
	public void move()
	{
		for(int j=0;j<COL;j++)
		{
			ser.SetValue(0, j, "@");
			if(j>0)
			ser.SetValue(0, j-1, " ");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	
	}
	public void UpdateProgress(int Progress)
	{
		ser.SetScreenProgrees(Progress);
	}
	
	public void UpdateLife(int Life)
	{
		ser.SetScreenLife(Life);
	}
	public void UpdateScore(int Score)
	{
		ser.SetScreenScore(Score);
	}
	public void UpdateClock(Clock ClockList[])
	{
		if(ClockList[0]==null)
		{
			ser.SetScreenClockMalos(" ");
		}
		else
		{
			ser.SetScreenClockMalos(ClockList[0].toString());
		}
		if(ClockList[1]==null)
		{
			ser.SetScreenClockGift(" ",0);
		}
		else
		{
			ser.SetScreenClockGift(ClockList[1].toString(),0);
		}
	}
	
	
	
	public void setFocusableOn()
	{
		this.setFocusable(true);
	}
	public void PrintToBoard(CellData BoardValues[][])
	{
		for(int i=0;i<ROW;i++)
		{
			for(int j=0;j<COL;j++)
			{
				ser.SetValue(i, j,BoardValues[i][j].toString());
				ser.SetColorBackg(i, j, BoardValues[i][j].GetColorBack());
				ser.SetColorForg(i, j, BoardValues[i][j].GetColorForg());
			}
		}
		this.repaint();
	}
	
	public void SetValue(int i,int j, char value)
	{
		ser.SetValue(i, j,""+value);
	}
	
	public boolean isEmpty()
	{
		return charList.isEmpty();
	}
	public char NextChar()
	{
		char ch = charList.get(0);
		charList.remove(0);
		return ch;
	}
	
	@Override
	public void keyPressed(KeyEvent e) 
	{
		switch(e.getKeyCode())
		{
			case 39:
			case 102:
			case 68: charList.add('r');		//
			break;
			case 37:
			case 100:
			case 65:charList.add('l');	//Left
				break;
			case 40:
			case 98:
			case 83: charList.add('d');	//down
				break;
			case 38:
			case 104:
			case 87:charList.add('u');	 //up
				break;
		}
		//System.out.println("The char is = " + e.getKeyChar());
		//System.out.println("The scan is = " + e.getKeyCode());
	}
	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
	


	
	/*public void WelcomeScreen()
	{
		int i,difficulty=0;
		char color;
		char mask=1;
		while(difficulty==0)
		{
			if(flagUpdate==1)
			{
				if(mask==8) mask=1; 
				color = mask<<4;
				mask++;
				for(i=0;i<SCR_LENGTH;i+=6)
				{
					Screen[i]=USER;
					Screen[i+1]=color;
				}
				Myprintf("C L O S I N G   A R E A S",SCR_COL*6 +54,11);
				Myprintf("The Orignal Game",SCR_COL*7 +62,11);
				Myprintf("Please Choose Game Difficulty levels",SCR_COL*11 +46,11);
				Myprintf(" 1 Beginner",SCR_COL*13 +68,9);
				Myprintf(" 2 Medium",SCR_COL*15 +68,10);
				Myprintf(" 3 Advanced",SCR_COL*17 +68,12);
				Myprintf("Press ESC to Exit",SCR_COL*19 +68,11);
				
				Myprintf(" (C) Shimon&Ron <-> Productions",SCR_COL*(SCR_ROW-2)+100,13);
				flagUpdate=0;
				if(front != -1)
				{
					char scanCode = InputArr[front];
					front = rear = -1;
					if(scanCode==79 || scanCode==2)  // Press 1
					{
						difficulty=1;
					}
					else if(scanCode==3 || scanCode==80) // Press 2 
					{
						difficulty=2;
					}
					else if(scanCode==4 || scanCode==81)  // Press 3
					{
						difficulty=3;
					}	
					else if(scanCode == 1)	
					{
						End=0; difficulty=4;
					}
				}
			}
			
		}
		ClearScreen();
		Level=1;
		Difficulty=difficulty;
	}
	void Myprintf(char str[],int position,int color)
	{
		int i=0,j=position;
		while(str[i]!='\0')
		{
			Screen[j]=str[i];
			Screen[j+1]=color;
			j+=2;
			i++;
		}
	}

	void ClearScreen()
	{
	int i;
		for(i=0;i<LENGTH;i+=2)
		{
				TemporaryScreen[i]=' ';
				TemporaryScreen[i+1]=FRAME_NOT_FILL_COLOR;
		}
		for(i=0;i<SCR_LENGTH;i+=2)
		{
		    Screen[i]=' ';
			Screen[i+1]=FRAME_NOT_FILL_COLOR;
		}
	}
	
	
	void DisplayScreen()
{
	if(DisableDisplay==0)
	{
		int i,temp;
		int j;
		char fisrtLine[COL];
		char lastLine[COL];
		int size;
		// print the first line, the : Life, Score,Progress 
		size = sprintf(fisrtLine,"           %c X %d              Score : %d              Progress : %d %%",USER,Life,Score,Progress);
		for(i=0,j=0;j<size;i+=2,j++)
		{
		  Screen[i]=fisrtLine[j];
		  Screen[i+1] = 7;
		}	
		i=SCR_COL;
		for(j=0;i<LENGTH;i++,j++)
		{
			Screen[i]=TemporaryScreen[j];
		}
		for(j=0;j<SCR_COL;j++,i++)
		{
			Screen[i]=TimeStr[j];
		}
	}
}

oid NextLevelScreen()
{
	char str[20];
	int i,mask,color,flag=1;
	int timeToBlink=1;
	sprintf(str,"L E V E L   %d    ",Level); //USER,Life,Score,Progress);
	DisableDisplay=1;
	ClearScreen();
	while(timeToBlink)
	{
		if(mask==8) 
				mask=1; 
		color = mask<<4;
		mask++;
		
		if(flagUpdate==1)
		{
			// Blink the string "L E V E L XX"
			if(flag) 
				Myprintf(str,SCR_COL*6 +66,15);  //Show string
			else 
				Myprintf("                  ",SCR_COL*6 +60,0); // delete string
			flag = 1 - flag;
			
			//Blink Frame
			for(i=0;i<SCR_COL;i+=2)
			{
				//first line
				Screen[i+1]=color;   //Attribute (in the odd: Attribute)
				// Second line
				Screen[i+1+SCR_COL]=color;
			}
			// set the Right and Left boarder
			for(i=2;i<SCR_ROW-2;i++)
			{
			   //Left boarder 
			   Screen[i*SCR_COL+1]=color;
			   Screen[i*SCR_COL+3]=color;
			   //Right boarder 
			   Screen[(i+1)*SCR_COL-1]=color;
			   Screen[(i+1)*SCR_COL-3]=color;
			}
			//set the Lower boarder, first and Second line for the end  
			for(i=0;i<SCR_COL;i+=2)
			{
				//first line
				Screen[ i+1+(SCR_ROW-3)*SCR_COL]=color;   //Attribute (in the odd: Attribute)
				// Second line
				Screen[ i+1+(SCR_ROW-2)*SCR_COL]=color;
			}
			flagUpdate=0;
			Myprintf("Press Any Key To Continue...",SCR_COL*15 +50,11);
			// if press key
			if(front != -1)
			{
				front = rear = -1;
				timeToBlink=0;
			}
		}
	}
	DisableDisplay=0;
	// clear the screen
	ClearScreen();
	// set the new level
	SetNewLevelFrame();
}
void GameOverScreen()
{
		int i,j,color=64;   //j=number of intreation
		char scoreStr[25];
		int startPos;
		for(j=0;j<13;j++)
		{
			while(flagUpdate==0);
			flagUpdate=0;
			//Blink Frame
			for(i=0;i<SCR_COL;i+=2)
			{
				//first line
				Screen[i+j*SCR_COL]=' ';
				Screen[i+1+j*SCR_COL]=color;   //Attribute (in the odd: Attribute)
			}
			
			// set the Right and Left boarder
			for(i=0;i<SCR_ROW-1;i++)
			{
			   //Left boarder 
			   Screen[i*SCR_COL+j*2]=' ';
			   Screen[i*SCR_COL+1+j*2]=color;
			   //Right boarder 
			   Screen[(i+1)*SCR_COL-2-j*2]=' ';
			   Screen[(i+1)*SCR_COL-1-j*2]=color;
			}
			
			//set the Lower boarder, first and Second line for the end  
			for(i=0;i<SCR_COL;i+=2)
			{
				//first line
				Screen[i+(SCR_ROW-2-j)*SCR_COL]=' ';
				Screen[ i+1+(SCR_ROW-2-j)*SCR_COL]=color;   //Attribute (in the odd: Attribute)
			}	
		}
		//Game Over
		i=j=0;
		while(i<8)
		{
			char str[][8]={"G","A","M","E","O","V","E","R"};
			while(flagUpdate==0);
			flagUpdate=0;
			if(j==4) 
				j+=3;
			Myprintf(str[i],SCR_COL*8 + 58 + j*4,color|15);
			i++;
			j++;
		}
		startPos=64;
		while(flagUpdate==0); flagUpdate=0;
		sprintf(scoreStr,"Score  =  %d",Score);
		Myprintf(scoreStr,SCR_COL*12 + startPos,color|15);
		while(flagUpdate==0); flagUpdate=0;
		sprintf(scoreStr,"Level  =  %d",Level);
		Myprintf(scoreStr,SCR_COL*14 + startPos,color|15);
		if(Life>0)
		{
			while(flagUpdate==0); flagUpdate=0;
			sprintf(scoreStr,"Life  =  %d",Life);
			Myprintf(scoreStr,SCR_COL*16 + startPos,color|15);
			while(Life>0)
			{
				while(flagUpdate==0); flagUpdate=0;
				Score+=1000;
				Life--;
				sprintf(scoreStr,"Score  =  %d",Score);
				Myprintf(scoreStr,SCR_COL*12 + startPos,color|15);
				sprintf(scoreStr,"Life  =  %d    ",Life);
				Myprintf(scoreStr,SCR_COL*16 + startPos,color|15);
			}
			Myprintf("              ",SCR_COL*16 + startPos,color);
		}
		Myprintf("Press Any Key To Continue...",SCR_COL*17 +50,color|15);
}


void showYouWinScreen()
{
		int i,j,color=1,mask=1;   //j=number of intreation
		char scoreStr[25];
		int startPos;
		char str[]="Congratulations you win the game";
		for(j=0;j<13;j++)
		{
			if(mask==7) 
				mask=1; 
			color = (mask<<4);
			mask++;
				
			while(flagUpdate==0);
			flagUpdate=0;
			//Blink Frame
			for(i=0;i<SCR_COL;i+=2)
			{
				//first line
				Screen[i+j*SCR_COL]=' ';
				Screen[i+1+j*SCR_COL]=color;   //Attribute (in the odd: Attribute)
			}
			
			// set the Right and Left boarder
			for(i=0;i<SCR_ROW-1;i++)
			{
			   //Left boarder 
			   Screen[i*SCR_COL+j*2]=' ';
			   Screen[i*SCR_COL+1+j*2]=color;
			   //Right boarder 
			   Screen[(i+1)*SCR_COL-2-j*2]=' ';
			   Screen[(i+1)*SCR_COL-1-j*2]=color;
			}
			
			//set the Lower boarder, first and Second line for the end  
			for(i=0;i<SCR_COL;i+=2)
			{
				//first line
				Screen[i+(SCR_ROW-2-j)*SCR_COL]=' ';
				Screen[ i+1+(SCR_ROW-2-j)*SCR_COL]=color;   //Attribute (in the odd: Attribute)
			}	
		}
		//Game Over
		i=j=0;
		while(i<9)
		{
			while(flagUpdate==0); flagUpdate=0;
			if(i%2==0) Myprintf(str,SCR_COL*8 + 46 + j*4,Screen[SCR_COL*8+1]|15);
			else  Myprintf("                                          ",SCR_COL*8 + 46 + j*4,Screen[SCR_COL*8+1]|15);
			startPos=64;
			if(i==0) while(flagUpdate==0); flagUpdate=0;
			sprintf(scoreStr,"Score  =  %d",Score);
			Myprintf(scoreStr,SCR_COL*12 + startPos,Screen[SCR_COL*12+1]|15);
			if(i==0) while(flagUpdate==0); flagUpdate=0;
			sprintf(scoreStr,"Level  =  %d",Level);
			Myprintf(scoreStr,SCR_COL*14 + startPos,Screen[SCR_COL*14+1]|15);
			if(Life>0)
			{
				while(flagUpdate==0); flagUpdate=0;
				sprintf(scoreStr,"Life  =  %d",Life);
				Myprintf(scoreStr,SCR_COL*16 + startPos,Screen[SCR_COL*16+1]|15);
				while(Life>0)
				{
					while(flagUpdate==0); flagUpdate=0;
					Score+=1000;
					Life--;
					sprintf(scoreStr,"Score  =  %d",Score);
					Myprintf(scoreStr,SCR_COL*12 + startPos,Screen[SCR_COL*12+1]|15);
					sprintf(scoreStr,"Life  =  %d    ",Life);
					Myprintf(scoreStr,SCR_COL*16 + startPos,Screen[SCR_COL*16+1]|15);
				}
				Myprintf("              ",SCR_COL*16 + startPos,Screen[SCR_COL*16+1]);
			}
			i++;
		}
		Myprintf("Press Any Key To Continue...",SCR_COL*17 +50,Screen[SCR_COL*17+1]|15);
		
}
	
	void FillBlock()
{
	int i;
	for(i=0;i<NumberOfMonsters;i++)
	{
		if(MonstersArray[i].type == INSIDE_DIAGONAL_MONSTER || MonstersArray[i].type ==EATER_MONSTER)
		{
		   Mark(MonstersArray[i].indexPosition);  
		}
	}
	for(i=0;i<LENGTH;i+=2)
	{
		switch((TemporaryScreen[i+1]&BACK_MASK))
		{
			case FRAME_NOT_FILL_COLOR:
			case CHASING_LINE_COLOR:
			case NEW_LINE_BLOCK_COLOR: 
				if(isGiftAscii(TemporaryScreen[i]))
				{
					DeleteGift();
				}
				TemporaryScreen[i+1]=FRAME_FILL_COLOR; 	
				break;
			case TEMPORERY_MARK: TemporaryScreen[i+1]=(TemporaryScreen[i+1]&DELETE_COLOR_MASK)|FRAME_NOT_FILL_COLOR; break;
		}
	}
	UpdateProgress();
}
void Mark(int index)
{
	if( (TemporaryScreen[index+1]&BACK_MASK==FRAME_FILL_COLOR)     || 
		(TemporaryScreen[index+1]&BACK_MASK==NEW_LINE_BLOCK_COLOR) || 
		(TemporaryScreen[index+1]&BACK_MASK==CHASING_LINE_COLOR)   || 
		(TemporaryScreen[index+1]&BACK_MASK==TEMPORERY_MARK) )
		return;
	TemporaryScreen[index+1]=(TemporaryScreen[index+1]&DELETE_COLOR_MASK)|TEMPORERY_MARK;
	if( (TemporaryScreen[index-2+1]&BACK_MASK)==FRAME_NOT_FILL_COLOR)    Mark(index-2);
	if( (TemporaryScreen[index+2+1]&BACK_MASK)==FRAME_NOT_FILL_COLOR)    Mark(index+2);
	if( (TemporaryScreen[index+COL+1]&BACK_MASK)==FRAME_NOT_FILL_COLOR)  Mark(index+COL);
	if( (TemporaryScreen[index-COL+1]&BACK_MASK)==FRAME_NOT_FILL_COLOR)  Mark(index-COL);
}
	
	*/

